package com.hsd.znsh.service.impl;

import com.hsd.znsh.converter.BatchConverter;
import com.hsd.znsh.entity.Batch;
import com.hsd.znsh.entity.Farm;
import com.hsd.znsh.entity.Host;
import com.hsd.znsh.entity.Pick;
import com.hsd.znsh.enums.BatchStatusEnum;
import com.hsd.znsh.enums.DeletedStatusEnum;
import com.hsd.znsh.enums.ResultEnum;
import com.hsd.znsh.exception.ZnshException;
import com.hsd.znsh.pojo.output.BatchVO;
import com.hsd.znsh.pojo.query.BatchQuery;
import com.hsd.znsh.repository.*;
import com.hsd.znsh.service.BatchService;
import com.hsd.znsh.utils.DateUtil;
import com.hsd.znsh.utils.HqlUtil;
import com.hsd.znsh.utils.SecurityUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

/*
 * @auther: wangjiacai
 *
 * @create: 2018-04-26 14:44:09
 */
@Service
public class BatchServiceImpl extends BaseServiceImpl implements BatchService {

    @Autowired
    private BatchRepository batchRepository;

    @Autowired
    private FarmRepository farmRepository;

    @Autowired
    private PickRepository pickRepository;

    @Autowired
    private HostRepository hostRepository;

    @Override
    public long getBatchListTotalPages(BatchQuery batchQuery, Integer userId) {
        return this.count(HqlUtil.getCountHql(batchQuery),batchQuery);
    }

    @Override
    public List<BatchVO> getBatchList(BatchQuery batchQuery, Pageable pageable, Integer userId) {
        List<Batch> list=this.find(HqlUtil.getQueryHql(batchQuery),batchQuery,pageable);
        return BatchConverter.converterToBachVOList(list,hostRepository);
    }

    @Override
    public List<BatchVO> getBatchListByHostSn(String hostSn) {
        Host host=hostRepository.findByHostSn(hostSn);
        List<Batch> list=batchRepository.findByHostIdOrderByBatchSnDesc(host.getId());
        return BatchConverter.converterToBachVOList(list,hostRepository);
    }

    @Override
    public List<BatchVO> getBatchListByUser(Integer userId) {
        List<Integer> ids=hostRepository.findIdListByManagerId(userId);
        List<Batch> list=batchRepository.findByHostIdInOrderByBatchSnDesc(ids);
        return BatchConverter.converterToBachVOList(list,hostRepository);
    }

    @Override
    public Batch addBatch(Batch batch, String hostSn) {
        if(StringUtils.isEmpty(hostSn)) throw new ZnshException("地皮不能为空");
        Host host=hostRepository.findByHostSn(hostSn);
        String batchSn=batchRepository.findMaxBatchSnByHostId(host.getId());
        batch.setHostId(host.getId());
        batch.setBatchSn(getBatchSn(batchSn));
        batch.setStatus(BatchStatusEnum.NOT_START.getCode());
        return batchRepository.save(batch);
    }

    private String getBatchSn(String batchSn){
        if(StringUtils.isEmpty(batchSn)){
            return DateUtil.format(new Date(),"yyyyMMdd")+"-000001";
        }
        String dateNum=batchSn.substring(0,batchSn.indexOf("-"));
        Integer number=Integer.parseInt(batchSn.substring(batchSn.indexOf("-")+1));
        if(dateNum.equals(DateUtil.format(new Date(),"yyyyMMdd"))){
            return dateNum+"-"+getNumber(number+1);
        }
        return DateUtil.format(new Date(),"yyyyMMdd")+"-000001";
    }

    private String getNumber(Integer number){
        String string=number+"";
        while(string.length()<6){
            string="0"+string;
        }
        return string;
    }

    @Override
    public BatchVO getBatchInfo(Integer id) {
        Batch batch=batchRepository.findOne(id);
        return BatchConverter.converterToBachVO(batch,hostRepository);
    }

    @Override
    public void deleteBatch(Integer id) {
        List<Farm> farmList=farmRepository.findByBatchIdOrderByCreateDateTimeDesc(id);
        if(farmList.size()>0) throw new ZnshException(ResultEnum.FARM_EXITS);
        List<Pick>  pickListt=pickRepository.findByBatchIdOrderByPickSnDesc(id);
        if(pickListt.size()>0) throw new ZnshException(ResultEnum.PICK_EXITS);
        batchRepository.delete(id);
    }

    @Override
    public Batch finishBatch(Integer id) {
        Batch batch=batchRepository.findOne(id);
        batch.setStatus(BatchStatusEnum.ENDED.getCode());
        return batchRepository.save(batch);
    }
}
